home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
indents.zip
/
indent.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-30
|
60KB
|
1,441 lines
/**
* Copyright (c) 1985 Sun Microsystems, Inc.
* Copyright (c) 1980 The Regents of the University of California.
* Copyright (c) 1976 Board of Trustees of the University of Illinois.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted provided
* that the above copyright notice and this paragraph are duplicated in all
* such forms and that any documentation, advertising materials, and other
* materials related to such distribution and use acknowledge that the
* software was developed by the University of California, Berkeley, the
* University of Illinois, Urbana, and Sun Microsystems, Inc. The name of
* either University or Sun Microsystems may not be used to endorse or
* promote products derived from this software without specific prior written
* permission. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
* OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "globals.h"
#include "codes.h"
#ifndef lint
# ifndef ANSIC
char copyright[] =
"@(#) Copyright 1989 Object Design, Inc.\n\
@(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\
@(#) Copyright (c) 1980 The Regents of the University of California.\n\
@(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\
All rights reserved.\n";
static char sccsid[] = "@(#)indent.c 6.0 (Berkeley) 92/06/15";
# endif /* ANSIC */
#endif /* not lint */
#include <string.h>
#include <stdlib.h>
#ifdef BSD
#include <sys/param.h>
#include <unistd.h>
#endif /* BSD */
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif /* MAXPATHLEN */
#include <ctype.h>
#define free(ptr) if (ptr != NULL) {(void) free(ptr); ptr = NULL;} /* think about it. PETER */
char *in_name = "Standard Input"; /* will always point to name of
* input file */
char *out_name = "Standard Output"; /* will always point to name
* of output file */
char bakfile[MAXPATHLEN] = "";
#ifdef ANSIC
static void mainloop(void);
static void err(char *msg);
static void bakcopy(void);
static void usage(void);
#endif /* ANSIC */
#ifdef ANSIC
int main(int argc, char *argv[])
#else /* ANSIC */
main(argc, argv)
int argc;
char **argv;
#endif /* ANSIC */
{
int i; /* local loop counter */
char filename[MAXPATHLEN] = ""; /* input file name */
int fnamelength;/* input file name length */
FILE *stream;
/*--------------------------------------------------
COMMAND LINE SCAN
--------------------------------------------------*/
if (argc < 2) {
usage();
show_options = 1;
}
set_defaults();
if (show_options) {
list_options("Compile time defaults are:");
fprintf(stderr, "\n");
}
for (i = 1; i < argc; ++i)
if (strcmp(argv[i], "-npro") == 0)
break;
if (i >= argc)
set_profile(argv[0]);
for (i = 1; i < argc; ++i) {/* look through args (if any) for changes to
* defaults */
if (argv[i][0] == '-') {/* flag on parameter */
set_option(argv[i]);
}
}
if ((useStdio) || (show_options)) { /* only do stdio, ignore any
* specified files */
mainloop();
} else {
for (i = 1; i < argc; ++i) { /* look through args (if any) for
* filenames */
if (argv[i][0] != '-') { /* no flag on parameter */
if (argv[i][0] == '@' && strlen(argv[i]) > 2) {
/* it's a list of file names, get the filename */
fnamelength = 0;
filename[0] = 0;
while (argv[i][fnamelength + 1] != 0) {
filename[fnamelength] = argv[i][fnamelength + 1];
fnamelength++;
}
filename[fnamelength] = 0;
/* open the file */
fprintf(stderr, "Opening list file: %s.\n", filename);
if ((stream = fopen(filename, "r")) == 0) {
err(filename);
}
/* read the input file for file names */
while (!feof(stream)) {
filename[0] = 0;
fgets(filename, MAXPATHLEN - 1, stream);
if ((fnamelength = strlen(filename)) > 1) {
if (filename[fnamelength - 1] == '\n')
filename[fnamelength - 1] = 0; /* remove newline char */
in_name = filename; /* remember name of input
* file */
mainloop();
}
}
} else { /* assume it's a source file */
in_name = argv[i]; /* remember name of input file */
mainloop();
}
}
} /* end of for */
}
exit(0); /* closes the list file (and anything else) */
return 0; /* necessary to stop ANSI compilers moaning */
}
#ifdef ANSIC
static void mainloop(void)
#else /* ANSIC */
mainloop()
#endif /* ANSIC */
{
char *codebuf = NULL; /* buffer for code section */
char *labbuf = NULL; /* buffer for label */
char *combuf = NULL; /* buffer for comments */
extern int found_err; /* flag set in diag() on error */
int dec_ind; /* current indentation for declarations */
int di_stack[20]; /* a stack of structure indentation
* levels */
int flushed_nl; /* used when buffering up comments to
* remember that a newline was passed over */
int force_nl; /* when true, code must be broken */
int hd_type = 0;/* used to store type of stmt for if (...),
* for (...), etc */
register int i; /* local loop counter */
int scase; /* set to true when we see a case, so we will
* know what to do with the following colon */
int sp_sw; /* when true, we are in the expression of
* if(...), while(...), etc. */
int squest; /* when this is positive, we have seen a ?
* without the matching : in a <c>?<s>:<s>
* construct */
register char *t_ptr = NULL; /* used for copying tokens */
int type_code; /* the type of token, returned by lexi */
int last_else = 0; /* true iff last keyword was an else */
int just_saw_cc_cmnt = 0; /* true if '/''/' comment was
* just emitted (jrs 8jun92) */
int stringptr;
char save_com[sc_size] = ""; /* input text is saved here when
* looking for the brace after an
* if, while, etc. */
char *sc_end = NULL; /* pointer into save_com buffer */
/*-----------------------------------------------*\
| INITIALISATION |
\*-----------------------------------------------*/
else_or_endif = false; /* -cp PETER */
ps.p_stack[0] =